﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Surveying
{
    /// <summary>
    /// 点和线段关系判断：右手法则
    /// </summary>
    public class Point_Line_RelationClass
    {
        private Surveying.Point m_VPoint;
        private Surveying.Point m_LinePoint_1;
        private Surveying.Point m_LinePoint_2;
        
        public Surveying.Point VPoint
        {
            set { m_VPoint = value; }
        }
        public Surveying.Point LinePoint_1
        {
            set { m_LinePoint_1 = value; }
        }
        public Surveying.Point LinePoint_2
        {
            set { m_LinePoint_2 = value; }
        }
        /// <summary>
        /// 右手法则
        /// </summary>
        /// <returns></returns>
        public string RelationCal()
        {
            double H = m_VPoint.x * (m_LinePoint_1.y - m_LinePoint_2.y) - m_VPoint.y * (m_LinePoint_1.x - m_LinePoint_2.x) + m_LinePoint_1.x * m_LinePoint_2.y - m_LinePoint_2.x * m_LinePoint_1.y;
            string ret = "";
            if (H > 0)
            {                
                if (m_LinePoint_1.y < m_LinePoint_2.y)
                {
                    ret = "三个点成逆时针排列！参考点在线段左侧！";
                    if (m_VPoint.y <= m_LinePoint_2.y && m_VPoint.y >= m_LinePoint_1.y)
                    {
                        ret = ret + "在参考点正X射线方向上存在1个交点！";
                    }
                    else
                    {
                        ret = ret + "在参考点正X射线方向上没有交点！";
                    }
                }
                else
                {
                    ret = "三个点成逆时针排列！参考点在线段左侧！在参考点正X射线方向上没有交点！";
                }                
            }
            else if (H < 0)
            {
                
                if (m_LinePoint_1.y > m_LinePoint_2.y)
                {
                    ret = "三个点成顺时针排列！参考点在线段右侧！";
                    if (m_VPoint.y >= m_LinePoint_2.y && m_VPoint.y <= m_LinePoint_1.y)
                    {
                        ret = ret + "在参考点正X射线方向上存在1个交点！";
                    }
                    else
                    {
                        ret = ret + "在参考点正X射线方向上没有交点！";
                    }
                }
                else
                {
                    ret = "三个点成顺时针排列！参考点在线段右侧！在参考点正X射线方向上没有交点！";
                }     
            }

            return ret;
        }
        /// <summary>
        /// 1是逆时针排列，-1顺时针，0是相交
        /// </summary>
        /// <param name="vp"></param>
        /// <param name="lp1"></param>
        /// <param name="lp2"></param>
        /// <returns></returns>
        public int get_Relation_Point_Line(Surveying.Point vp, Surveying.Point lp1, Surveying.Point lp2)
        {
            double p = vp.x * (lp1.y - lp2.y) - vp.y * (lp1.x - lp2.x) + lp1.x * lp2.y - lp2.x * lp1.y;
            if (p > 0)
            {
                return 1;
            }
            else if (p < 0)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }
    }
}
